/**
 * @file MouseSimulator.cpp
 * @details 鼠标事件模拟器。
 * @copyright 北京经海科技有限公司
 * @author 李渊 <18069211@qq.com>
 * @version 1.0
 * @date 2020.02.05
 */
 
#include "MouseSimulator.h"

#include <windows.h>
#include <winuser.h>

MouseSimulator::MouseSimulator()
{

}

void MouseSimulator::click()
{
    MouseClick();
}

void MouseSimulator::doubleClick()
{
    MouseDoubleClick();
}

void MouseSimulator::click(QPoint t)
{
    POINT p;
    ::GetCursorPos(&p);
    MouseMove(t.x(), t.y());
    MouseClick();
    MouseMove(p.x, p.y);
}

void MouseSimulator::doubleClick(QPoint t)
{
    POINT p;
    ::GetCursorPos(&p);
    MouseMove(t.x(), t.y());
    MouseDoubleClick();
    MouseMove(p.x, p.y);
}

void MouseSimulator::mouseEvent(QMouseEvent e)
{

}


void MouseMove(int x, int y)//鼠标移动到指定位置
{
    double fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;//获取屏幕分辨率宽度
    double fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;//获取屏幕分辨率高度
    double fx = x * (65535.0f / fScreenWidth);
    double fy = y * (65535.0f / fScreenHeight);
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
    Input.mi.dx = fx;
    Input.mi.dy = fy;
    ::SendInput(1, &Input, sizeof(INPUT));
}

void MouseClick()
{
    INPUT input[2];
    int i = 0;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    input[i].mi.dwExtraInfo = 0;

    i = 1;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    input[i].mi.dwExtraInfo = 0;

    ::SendInput(2, input, sizeof(INPUT));
}

void MouseDoubleClick()
{
    INPUT input[4];
    int i = 0;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    input[i].mi.dwExtraInfo = 0;

    i = 1;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    input[i].mi.dwExtraInfo = 0;

    i = 2;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    input[i].mi.dwExtraInfo = 0;

    i = 3;
    input[i].type = INPUT_MOUSE;
    input[i].mi.mouseData = 0;
    input[i].mi.dwFlags = MOUSEEVENTF_LEFTUP;
    input[i].mi.dwExtraInfo = 0;

    ::SendInput(4, input, sizeof(INPUT));
}

void MouseLeftDown()//鼠标左键按下
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    ::SendInput(1, &Input, sizeof(INPUT));
}

void MouseLeftUp()//鼠标左键放开
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    ::SendInput(1, &Input, sizeof(INPUT));

}

void MouseRightDown()//鼠标右键按下
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
    ::SendInput(1, &Input, sizeof(INPUT));
}

void MouseRightUp()//鼠标右键放开
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_RIGHTUP;
    ::SendInput(1, &Input, sizeof(INPUT));
}
